var table;
$(function(){
    table=$("#customer").DataTable({
        "ajax": {
            url:"../../admin/customer/Get",
            type:"POST",
            data:function(d){
                d.name=$("#search_value").val()
                d.gender=$("#search_type").val()
            },error:function(XMLHttpRequest,textStatus,errorThrown){
                console.log("载入数据出错");
            }
        },
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": true,
        "bAutoWidth": true,
        "bInfo": true,//页脚信息
        "bFilter": false,//禁用搜索
        "bSort":false,//禁用排序
        "aLengthMenu": [2,3,300],
        "sPaginationType":'full_numbers',
        "columns": [
            {"data":"id"},
            { "data": "name" },
            { "data": "gender" },
            { "data": "birthday" },
            { "data": "id" },
        ],
        "aoColumnDefs": [ {
            "aTargets": [ 2 ],
            "mRender": function ( data, type, full ) {
                switch (data){
                    case 1:return "男";break;
                    case 2:return "女";break;
                    default :return "其他";break;
                }
            }
        },{
            "aTargets": [ 3 ],
            "mRender": function ( data, type, full ) {

                return data
            }
        },
            {
                "aTargets": [ 4 ],
                "mRender": function ( data, type, full ) {
                    return '<a onclick="editData(\''+full.id+'\',\''+full.name+'\',\''+full.gender+'\')">修改</a>' +
                        '<a style="margin-left: 12px" onclick="delData(\''+full.id+'\')">删除</a>';
                }
            } ],
        "fnRowCallback": function (nRow, aData, iDisplayIndex) {
            $("td:first", nRow).html(iDisplayIndex + 1);
            return nRow;
        },
        "oLanguage": {
            "sProcessing": "处理中...",
            "sLengthMenu": "每页 _MENU_ 条",
            "sZeroRecords": "没有匹配结果",
            "sInfo": " _START_ 至 _END_ 项结果，共 _TOTAL_ 项",//显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项
            "sInfoEmpty": "",//显示第 0 至 0 项结果，共 0 项
            "sInfoFiltered": "",//(由 _MAX_ 项结果过滤)
            "sInfoPostFix": "",
            "sSearch": "搜索：",
            "sUrl": "",
            "sEmptyTable": "没有数据呀!",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            }
        },
    });
})
//偏好使用HTML
new SMValidator('#addFrom');

function searchBtn(){
    table.draw();
}
//初始化新增
function addBtn(){
    $(".modal-title").text("新增属性")
    $("#id").val("")
    $("#add_name").val("")
    $("#add_gender").find("option[value='"+1+"']").attr("selected",true);
    $("#myModal").modal("show")
}
//modal框提交
function modal_sub(){
    if(SMValidator.validate('#addFrom')){
        $.ajax({
            url: "../../admin/customer",
            type: "POST",
            dataType: "json",
            data: $("#addFrom").serialize(),
            beforeSend: function () {
            },
            success: function (data,status) {
                console.log(status);
                if(data){
                    console.log(data);
                    Lobibox.notify('success', {
                        msg: '新增成功',
                        sound: false
                    });
                    $("#myModal").modal("hide")
                    table.draw();
                }else {
                    console.log(data)
                }
            },
            error: function (data) {
                if(!data){
                    Lobibox.notify('error', {
                        msg: '新增失败',
                        sound: false
                    });
                }
            }
        });
    }
}
//初始化修改
function editData(id,val,type){
    console.log(id);
    $(".modal-title").text("修改属性")
    $("#id").val(id)
    $("#add_name").val(val)
    $("#add_gender").find("option[value='"+type+"']").attr("selected",true);
    $("#myModal").modal("show")
}

function delData(id){
    Lobibox.confirm({
        msg: "确认要删除这条属性?",
        title:"删除属性",
        iconClass: false,
        callback: function ($this, type, ev) {
            if(type){
                $.ajax({
                url: "../../admin/customer/"+id+"?_csrf="+$("input[name=_csrf]").val(),//spring_security.csrf
                type: "DELETE",
                dataType: "json",
                success: function (data) {
                    if(data){
                        table.draw();
                        Lobibox.notify('success', {
                            msg: '删除成功',
                            sound: false
                        });
                    }else {
                        Lobibox.notify('error', {
                            msg: '删除成功',
                            sound: false
                        });
                    }
                }, error: function () {
                }
            });
            }
        }
    });
}
//时间格式转换
Date.prototype.Format = function(fmt)
{ //author: meizz
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "h+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
};
